﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServerNodeLib
{
	/// <summary>
	/// Represents a client node.
	/// This is the data that is persisted in the database.
	/// </summary>
	[Serializable()]
	public class Node
	{
		public Node()
		{
			actionRequests = new List<ActionRequest>();
			State = NodeState.Idle;
			StatusDescription = String.Empty;
			PercentComplete = -1;
			StartTime = DateTime.Today;//DateTime.MinValue;
			EndTime = DateTime.Today.AddDays(1);//DateTime.MaxValue;
			LastSeen = DateTime.Now;//DateTime.MinValue;
			IpAddress = String.Empty;
			NodeName = String.Empty;
			NodeId = Guid.NewGuid();
			BatchId = Guid.Empty;
			AcceptNewJobs = false;
			ErrorTimeout = 0;
			NormalTimeout = 0;
		}
		public void AcceptNewStatus(NodeStatus newStatus)
		{
			this.State = newStatus.State;
			this.StatusDescription = newStatus.StatusDescription;
			this.PercentComplete = newStatus.PercentComplete;
			this.StartTime = newStatus.StartTime;
			this.EndTime = newStatus.EndTime;
			this.IpAddress = newStatus.IpAddress;
			this.NodeName = newStatus.NodeName;
			this.NodeId = newStatus.NodeId;
			this.BatchId = newStatus.BatchId;
			this.AcceptNewJobs = newStatus.AcceptNewJobs;
			this.ErrorTimeout = newStatus.ErrorTimeout;
			this.NormalTimeout = newStatus.NormalTimeout;
			this.ResponseQueue = new List<Guid>();
			this.ResponseQueue.InsertRange(0,newStatus.ResultQueue);
		}
		public NodeStatus GetNodeStatus()
		{
			NodeStatus ns = new NodeStatus();
			ns.AcceptNewJobs = this.AcceptNewJobs;
			ns.BatchId = this.BatchId;
			ns.EndTime = this.EndTime;
			ns.ErrorTimeout = this.ErrorTimeout;
			ns.IpAddress = this.IpAddress;
			ns.NodeId = this.NodeId;
			ns.NodeName = this.NodeName;
			ns.NormalTimeout = this.NormalTimeout;
			ns.PercentComplete = this.PercentComplete;
			ns.StartTime = this.StartTime;
			ns.State = this.State;
			ns.StatusDescription = this.StatusDescription;
			ns.ResultQueue = new List<Guid>();
			ns.ResultQueue.InsertRange(0,this.ResponseQueue);
			return ns;
		}
		public NodeState State;
		public String StatusDescription;
		public double PercentComplete;
		public DateTime StartTime;
		public DateTime EndTime;
		public DateTime LastSeen;
		public String IpAddress;
		public String NodeName;
		public Guid NodeId;
		public Guid BatchId;
		public bool AcceptNewJobs;
		public int ErrorTimeout;
		public int NormalTimeout;
		public List<Guid> ResponseQueue;
		//public DataAdapter.LoadActionRequests OnLoadActionRequests;
		private List<ActionRequest> actionRequests;
		public List<ActionRequest> ActionRequests
		{
			get
			{
				/*
				if (actionRequests == null)
				{
					// lazy load
					if (OnLoadActionRequests != null)
					{
						actionRequests = OnLoadActionRequests.Invoke(this.NodeId);
					}
				}
				*/
				return actionRequests;
			}
			set
			{
				if (value != null)
					actionRequests = value;
			}
		}

		public override string ToString()
		{
			String stateStr = State.ToString();
			if (State != NodeState.Idle)
				stateStr += " " + StatusDescription;
			return string.Format(ServerNodeSettings.Default.NodeStringFormat,
				NodeName,
				stateStr,
				(AcceptNewJobs ? "On" : "Off")
				);
		}
	}
}
